home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 May / EnigmA AMIGA RUN 27 (1998)(G.R. Edizioni)(IT)[!][issue 1998-05].iso / recent1 / gifani.lha / gifanim_datatype / misc.c < prev    next >
C/C++ Source or Header  |  1998-04-13  |  10KB  |  368 lines

  1.  
  2. /*
  3. **
  4. **  $VER: misc.c 2.2 (13.4.98)
  5. **  gifanim.datatype 2.2
  6. **
  7. **  Misc functions
  8. **
  9. **  Written 1997/1998 by Roland 'Gizzy' Mainz
  10. **  Original example source from David N. Junod
  11. **
  12. */
  13.  
  14. /* main includes */
  15. #include "classbase.h"
  16.  
  17. /*****************************************************************************/
  18.  
  19. /* clib sprintf replacement */
  20. void mysprintf( struct ClassBase *cb, STRPTR buffer, STRPTR fmt, ... )
  21. {
  22.     APTR args;
  23.  
  24.     args = (APTR)((&fmt) + 1);
  25.  
  26.     RawDoFmt( fmt, args, (void (*))"\x16\xc0\x4e\x75", buffer );
  27. }
  28.  
  29. /*****************************************************************************/
  30.  
  31. /* translate IBM PC character set to ISO Latin1. Limitted to 7 bit chars, except some german specific */
  32. void IBMPC2ISOLatin1( STRPTR ibmpc, STRPTR isolatin1 )
  33. {
  34.     /* the following must be unsigned */
  35.     register UBYTE *from = (UBYTE *)ibmpc,
  36.                    *to   = (UBYTE *)isolatin1;
  37.  
  38.     if( from && to )
  39.     {
  40.       do
  41.       {
  42.         register UBYTE ch = *from;
  43.  
  44.         switch( ch )
  45.         {
  46.           case 132: *(to++) = 'ä'; break;
  47.           case 148: *(to++) = 'ö'; break;
  48.           case 129: *(to++) = 'ü'; break;
  49.           case 142: *(to++) = 'Ä'; break;
  50.           case 153: *(to++) = 'Ö'; break;
  51.           case 154: *(to++) = 'Ü'; break;
  52.           case 225: *(to++) = 'ß'; break;
  53.           case   9:                       /* tab */
  54.           case  10: break;                /* ignore LF to filter CTRL/LF-sequences */
  55.           default:
  56.           {
  57.               if( ch < 128U )
  58.               {
  59.                 *to++ = ch;
  60.               }
  61.               else
  62.               {
  63.                 *to++ = '_'; /* can't convert */
  64.               }
  65.           }
  66.               break;
  67.         }
  68.       } while( *from++ );
  69.     }
  70. }
  71.  
  72.  
  73. /*****************************************************************************/
  74.  
  75. /* copy a given IFF CMAP chunk into a picture.datatype or animation.datatype object */
  76. BOOL CMAP2Object( struct ClassBase *cb, Object *o, UBYTE *rgb, ULONG rgbsize )
  77. {
  78.     struct ColorRegister *acm;
  79.     ULONG                *acregs;
  80.     ULONG                 nc;
  81.  
  82.     /* file has this many colors (e.g. each color has one byte per R,B,G-gun) */
  83.     nc = rgbsize / 3UL;
  84.  
  85.     SetDTAttrs( o, NULL, NULL, ADTA_NumColors, nc, TAG_DONE );
  86.  
  87.     /* Get color context */
  88.     if( GetDTAttrs( o,
  89.                     ADTA_ColorRegisters, (&acm),
  90.                     ADTA_CRegs,          (&acregs),
  91.                     ADTA_NumColors,      (&nc),
  92.                     TAG_DONE ) == 3UL )
  93.     {
  94.       /* All valid ? */
  95.       if( acm && acregs && nc )
  96.       {
  97.         ULONG i;
  98.  
  99.         for( i = 0UL ; i < nc ; i++, acm++ )
  100.         {
  101.           acm -> red   =  *rgb++;
  102.           acm -> green =  *rgb++;
  103.           acm -> blue  =  *rgb++;
  104.  
  105.           /* Replicate the color information.
  106.            * This surrounds an OS bug which uses the low-order bytes of the 32-bit colors
  107.            * instead of the high order ones
  108.            */
  109.           acregs[ ((i * 3) + 0) ] = ((ULONG)(acm -> red))   * 0x01010101UL;
  110.           acregs[ ((i * 3) + 1) ] = ((ULONG)(acm -> green)) * 0x01010101UL;
  111.           acregs[ ((i * 3) + 2) ] = ((ULONG)(acm -> blue))  * 0x01010101UL;
  112.         }
  113.  
  114.         return( TRUE );
  115.       }
  116.     }
  117.  
  118.     return( FALSE );
  119. }
  120.  
  121.  
  122. /* Create a ColorMap from a given IFF CMAP chunk */
  123. struct ColorMap *CMAP2ColorMap( struct ClassBase *cb, ULONG anumcolors, UBYTE *rgb, ULONG rgbsize )
  124. {
  125.     struct ColorMap *cm;
  126.     ULONG            a_nc   = anumcolors;     /* Number of colors in animation */
  127.     ULONG            rgb_nc = rgbsize / 3UL;  /* Number of colors in CMAP      */
  128.  
  129.     /* Get a colormap which hold all colors */
  130.     if( cm = GetColorMap( (long)MAX( a_nc, rgb_nc ) ) )
  131.     {
  132.       ULONG i,
  133.             r, g, b;
  134.  
  135.       for( i = 0UL ; i < rgb_nc ; i++ )
  136.       {
  137.         r = *rgb++;
  138.         g = *rgb++;
  139.         b = *rgb++;
  140.  
  141.         /* Replicate color information (see CMAP2Object for details) and store them into colormap */
  142.         SetRGB32CM( cm, i, (r * 0x01010101UL), (g * 0x01010101UL), (b * 0x01010101UL) );
  143.       }
  144.  
  145.       /* BUG: the remaining entries should be filled with colors from the last colormap */
  146.       for( ; i < a_nc ; i++ )
  147.       {
  148.         SetRGB32CM( cm, i, 0UL, 0UL, 0UL ); /* fill remaining entries with black */
  149.       }
  150.     }
  151.  
  152.     return( cm );
  153. }
  154.  
  155.  
  156. /* Clone a colormap */
  157. struct ColorMap *CopyColorMap( struct ClassBase *cb, struct ColorMap *src )
  158. {
  159.     struct ColorMap *dest = NULL;
  160.  
  161.     if( src )
  162.     {
  163.       ULONG *ctable;
  164.  
  165.       if( ctable = (ULONG *)AllocVec( ((ULONG)(src -> Count) * sizeof( ULONG ) * 3UL), MEMF_PUBLIC ) )
  166.       {
  167.         if( dest = GetColorMap( (long)(src -> Count) ) )
  168.         {
  169.           ULONG i;
  170.  
  171.           GetRGB32( src, 0UL, (ULONG)(src -> Count), ctable );
  172.  
  173.           for( i = 0UL ; i < (src -> Count) ; i++ )
  174.           {
  175.             SetRGB32CM( dest, i, ctable[ ((i * 3) + 0) ], ctable[ ((i * 3) + 1) ], ctable[ ((i * 3) + 2) ] );
  176.           }
  177.         }
  178.  
  179.         FreeVec( ctable );
  180.       }
  181.     }
  182.  
  183.     return( dest );
  184. }
  185.  
  186.  
  187. /*****************************************************************************/
  188.  
  189. /* write a chunkypixel array into a truecolor bitmap using a given palette (requires CyberGFX) */
  190. void WriteRGBPixelArray8( struct ClassBase *cb, struct BitMap *bm, ULONG animwidth, ULONG animheight, struct ColorRegister *cm, UBYTE *chunky )
  191. {
  192.              struct RastPort rp = { 0 };
  193.     register ULONG           x,
  194.                              y;
  195.  
  196.     /* Set up the temp. rastport */
  197.     InitRastPort( (&rp) );
  198.     rp . BitMap = bm;
  199.  
  200.     for( y = 0U ; y < animheight ; y++ )
  201.     {
  202.       for( x = 0U ; x < animwidth ; x++ )
  203.       {
  204.         register struct ColorRegister *cr = (&cm[ *chunky++ ]);
  205.  
  206.         /* The hack way: WriteRGBPPixel ignores the top byte (alpha channel) if the destination
  207.          * has no alpha channel. Therefore we use here the struct ColorRegister entry directly by
  208.          * moving the cr pointer one byte back and de-reference it to get the requested ULONG.
  209.          * Saves some SHIFT and OR operations...
  210.          */
  211.         WriteRGBPixel( (&rp), (UWORD)x, (UWORD)y, *((ULONG *)(((UBYTE *)cr) - 1)) );
  212.       }
  213.     }
  214. }
  215.  
  216.  
  217. /*****************************************************************************/
  218.  
  219. /* from animation.datatype V41.5 */
  220. static
  221. void XCopyMem( struct ClassBase *cb, APTR src, APTR dest, ULONG size )
  222. {
  223.     /* Check if we can use the optimized CopyMemQuick */
  224.     if( (ALIGN_LONG( src ) == src) && (ALIGN_LONG( dest ) == dest) )
  225.     {
  226.       register ULONG lsize = size & ~3UL,
  227.                      cut   = size - lsize; /* remaining bytes (0-3) */
  228.  
  229.       CopyMemQuick( src, dest, lsize );
  230.  
  231.       if( cut )
  232.       {
  233.         src  = ((UBYTE *)src)  + lsize;
  234.         dest = ((UBYTE *)dest) + lsize;
  235.  
  236.         CopyMem( src, dest, cut );
  237.       }
  238.     }
  239.     else
  240.     {
  241.       CopyMem( src, dest, size );
  242.     }
  243. }
  244.  
  245.  
  246. /* from animation.datatype V41.5 */
  247. /* Copy bm1 to bm2, planar version (interleaved and non-interleaved) */
  248. static
  249. void CopyBitMapPlanar( struct ClassBase *cb, struct BitMap *bm1, struct BitMap *bm2, ULONG widthbpr )
  250. {
  251.     ULONG  bpr1 = bm1 -> BytesPerRow;
  252.     ULONG  bpr2 = bm2 -> BytesPerRow;
  253.  
  254.     /* Same bitmap layout ? */
  255.     if( bpr1 == bpr2 )
  256.     {
  257.       /* Interleaved BitMap ? */
  258.       if( ((bm1 -> Planes[ 1 ]) - (bm1 -> Planes[ 0 ])) == (bpr1 / (ULONG)(bm1 -> Depth)) )
  259.       {
  260.         ULONG planesize = bpr2 * (ULONG)(bm2 -> Rows);
  261.  
  262.         XCopyMem( cb, (bm1 -> Planes[ 0 ]), (bm2 -> Planes[ 0 ]), planesize );
  263.       }
  264.       else
  265.       {
  266.         ULONG planesize = bpr2 * (ULONG)(bm2 -> Rows);
  267.         UWORD i;
  268.  
  269.         for( i = 0U ; i < (bm2 -> Depth) ; i++ )
  270.         {
  271.           XCopyMem( cb, (bm1 -> Planes[ i ]), (bm2 -> Planes[ i ]), planesize );
  272.         }
  273.       }
  274.     }
  275.     else
  276.     {
  277.       register UBYTE *src;
  278.       register UBYTE *dst;
  279.       register LONG   r;
  280.       register LONG   p;
  281.  
  282.       for( p = bm1 -> Depth - 1 ; p >= 0 ; p-- )
  283.       {
  284.         src = (BYTE *)bm1 -> Planes[ p ];
  285.         dst = (BYTE *)bm2 -> Planes[ p ];
  286.  
  287.         for( r = bm1 -> Rows - 1 ; r >= 0 ; r-- )
  288.         {
  289.           CopyMem( src, dst, widthbpr );
  290.           src += bpr1;
  291.           dst += bpr2;
  292.         }
  293.       }
  294.     }
  295. }
  296.  
  297.  
  298. /* from animation.datatype V41.5 */
  299. /* Copy bm1 to bm2, system/CyberGFX function*/
  300. static
  301. void CopyBitMapSystem( struct ClassBase *cb, struct BitMap *bm1, struct BitMap *bm2, ULONG width, ULONG height )
  302. {
  303.     /* Assumption: If a non-planar bitmap occurs BltBitMap should be able
  304.      * to blit it into a planar one
  305.      */
  306.     BltBitMap( bm1, 0L, 0L, bm2, 0L, 0L, width, height, 0xC0UL, 0xFFUL, NULL );
  307.  
  308.     WaitBlit();
  309. }
  310.  
  311.  
  312. void CopyBitMap( struct ClassBase *cb, struct BitMap *dest, struct BitMap *src, ULONG width, ULONG height )
  313. {
  314.     if( dest && src )
  315.     {
  316.       if( CyberGfxBase )
  317.       {
  318.         CopyBitMapSystem( cb, src, dest, width, height );
  319.       }
  320.       else
  321.       {
  322.         CopyBitMapPlanar( cb, src, dest, (width / 8UL) );
  323.       }
  324.     }
  325. }
  326.  
  327. /*****************************************************************************/
  328.  
  329. /* allocate a piece of memory from an exec memory pool and track the allocation size */
  330. APTR AllocVecPooled( struct ClassBase *cb, APTR pool, ULONG memsize )
  331. {
  332.     ULONG *memory = NULL;
  333.  
  334.     if( pool && memsize )
  335.     {
  336.       memsize += (ULONG)sizeof( ULONG );
  337.  
  338.       if( memory = (ULONG *)AllocPooled( pool, memsize ) )
  339.       {
  340.         (*memory) = memsize;
  341.  
  342.         memory++;
  343.       }
  344.     }
  345.  
  346.     return( (APTR)memory );
  347. }
  348.  
  349.  
  350. void FreeVecPooled( struct ClassBase *cb, APTR pool, APTR mem )
  351. {
  352.     if( pool && mem )
  353.     {
  354.       ULONG *memory;
  355.  
  356.       memory = (ULONG *)mem;
  357.  
  358.       memory--;
  359.  
  360.       FreePooled( pool, memory, (*memory) );
  361.     }
  362. }
  363.  
  364.  
  365. /*****************************************************************************/
  366.  
  367.  
  368.